// import { take, put, takeEvery, delay, call, cps, all, fork, cancel } from 'redux-saga/effects'
import { take, put, takeEvery, delay, call, cps, all, fork, cancel } from '@/redux-saga/effects'
import * as types from '../action-types';

let times = 10;
function* workSaga() {
    // yield take(types.ASYNC_ADD);
    while(times --) {
        yield delay(1000);
        yield put({ type: types.ADD })
    }
    // return 'workSaga1';
}
function* workSaga2() {
    yield take(types.ASYNC_ADD);
    yield put({ type: types.ADD })
    return 'workSaga2';
}
function* workSaga3() {
    yield take(types.ASYNC_ADD);
    yield put({ type: types.ADD })
    return 'workSaga3';
}
// const delay2 = (ms) => new Promise(res => {
//     setTimeout(res, ms)
// })

// function promise(arg) {
//     console.log(arg);
//     return new Promise(res => {
//         setTimeout(res, arg)
//     })
// }
// function cb(arg, next) {
//     console.log(arg, next);
//     next(null, 'aa');
// }


// rootSaga, workSaga we
function* rootSaga() {
    // const task = yield fork(workSaga);
    // console.log(task);   
    // yield take(types.CANCEL);
    // // 取消这个fork子进程
    // yield cancel(task);


    // const ret = yield all([workSaga(), workSaga2(), workSaga3()]);
    // console.log(ret);


    // yield take(types.ASYNC_ADD);
    // const r = yield cps(cb, 'hchlq');
    // console.log(r);


    // yield workSaga()

    // yield delay(2000);
    // yield take(types.ASYNC_ADD);
    // // 调用这个函数, 不会阻塞进程,后面作为参数传给第一个参数
    // yield call(workSaga, 1000);
    // yield put({ type: types.ADD })
    // yield takeEvery(types.ASYNC_ADD, workSaga)
    // while(true) { // while true就一直监听等待
    //     // 等待仓库派发async_add的动作后在next走到put
    //     yield take(types.ASYNC_ADD); // 只等待一次，然后就销毁了
    //     yield put({type: types.ADD});
    // }
}


export default rootSaga;